using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;


namespace UWBGL_XNA_Lib13
{
    public class UWB_Primitive
    {
        protected bool mVisible;
        protected Vector3 mVelocity;
        protected Color mFlatColor;
        protected Color mShadingColor;
        protected eFillMode mFillMode;
        protected eShadeMode mShadeMode;
        protected float mPointSize;
        protected bool mDrawBoundingVolume;
        protected bool mbBlendingEnabled;
        protected bool mbTexturingEnabled;
        protected String mTexFileName;

        public UWB_Primitive()
        {
            mVisible = true;
            mVelocity = Vector3.Zero;
            mFlatColor = Color.Black;
            mShadingColor = Color.White;
            mFillMode = eFillMode.fmSolid;
            mShadeMode = eShadeMode.smGouraud;
            mPointSize = 2f;
            mbBlendingEnabled = false;

        }

        public void draw(eLevelofDetail lod, UWB_DrawHelper drawHelper)
        {
            if (!mVisible)
                return;

            setupDrawAttributes(drawHelper);
            drawPrimitive(lod, drawHelper);

            if (isDrawingBoundingVolume())
                drawBoundingVolume(lod, drawHelper, new Color(255, 0, 0));
        }

        public virtual void update(float elapsedSeconds)
        {

        }

        public virtual void mouseDownVertex(int vertexID, float x, float y)
        {

        }

        public virtual void moveTo(float x, float y)
        {

        }

        public virtual Vector3 getLocation()
        {
            return Vector3.Zero;
        }

        public void setVisible(bool visible)
        {
            mVisible = visible;
        }

        public bool isVisible()
        {
            return mVisible;
        }

        public void setVelocity(Vector3 velocity)
        {
            mVelocity = velocity;
        }

        public Vector3 getVelocity()
        {
            return mVelocity;
        }

        public bool isStationary()
        {
            return (mVelocity.X <= UWB_Utility.zeroTolerance
                    && mVelocity.Y <= UWB_Utility.zeroTolerance
                    && mVelocity.Z <= UWB_Utility.zeroTolerance);
        }

        public void setFlatColor(Color color)
        {
            mFlatColor = color;
        }

        public Color getFlatColor()
        {
            return mFlatColor;
        }

        public void setShadingColor(Color color)
        {
            mShadingColor = color;
        }

        public Color getShadingColor()
        {
            return mShadingColor;
        }

        public void setFillMode(eFillMode fillMode)
        {
            mFillMode = fillMode;
        }

        public eFillMode getFillMode()
        {
            return mFillMode;
        }

        public void setShadeMode(eShadeMode shadeMode)
        {
            mShadeMode = shadeMode;
        }

        public eShadeMode getShadeMode()
        {
            return mShadeMode;
        }

        public void setPointSize(float size)
        {
            mPointSize = size;
        }

        public float getPointSize()
        {
            return mPointSize;
        }

        protected virtual void setupDrawAttributes(UWB_DrawHelper drawHelper)
        {
            drawHelper.resetAttributes();
            drawHelper.setColor1(mFlatColor);
            drawHelper.enableBlending(mbBlendingEnabled);
            
            drawHelper.setTextureInfo(ref mTexFileName);
            drawHelper.enableTexture(mbTexturingEnabled);
        }

        protected virtual void drawPrimitive(eLevelofDetail lod, UWB_DrawHelper drawHelper)
        {

        }

        public virtual UWB_BoundingVolume getBoundingVolume(eLevelofDetail lod)
        {
            return null;
        }

        public virtual void collisionResponse(UWB_Primitive other, Vector3 otherLocation)
        {
            Vector3 direction = getLocation() - otherLocation;
            if (direction == Vector3.Zero)
            {
                setVelocity(Vector3.Zero);
                return;
            }

            direction.Normalize();

            float mySpeed = getVelocity().Length();

            setVelocity(mySpeed * direction);
        }

        public void drawBoundingVolume(eLevelofDetail lod, UWB_DrawHelper drawHelper, Color color)
        {
            UWB_BoundingVolume boundingVolume = getBoundingVolume(lod);
            if (boundingVolume != null && drawHelper != null)
            {
                drawHelper.resetAttributes();
                drawHelper.setColor1(color);
                drawHelper.setShadeMode(eShadeMode.smFlat);
                drawHelper.setFillMode(eFillMode.fmWireframe);
                boundingVolume.draw(drawHelper);
            }
        }

        public void setDrawBoundingVolume(bool on)
        {
            mDrawBoundingVolume = on;
        }

        public bool isDrawingBoundingVolume()
        {
            return mDrawBoundingVolume;
        }

        public void enableBlending(bool on)
        {
            mbBlendingEnabled = on;
        }
        public bool isBlendingEnabled()
        {
            return mbBlendingEnabled;
        }

        public void setTextureFileName(String n)
        {
            mTexFileName = n;
        }
        public String getTextureFileName()
        {
            return mTexFileName;
        }

        public void enableTexturing(bool on)
        {
            mbTexturingEnabled = on;
        }

        public bool isTexturingEnabled()
        {
            return mbTexturingEnabled;
        }
    }
}
